package com.price.common.recyclerview

import android.util.SparseIntArray
import android.view.ViewGroup
import androidx.annotation.LayoutRes


/**
 */
abstract class BaseMultipleItemAdapter<T : IMultipleItem?, VH : BaseViewHolder> :
    BaseAdapter<T, VH> {
    /**
     * itemType及与它对应的布局文件的映射集合；
     */
    private val itemTypeLayouts = SparseIntArray()

    constructor()
    constructor(datas: MutableList<T>?) : super(datas)

    override fun getItemViewType(position: Int): Int {
        if (mDatas != null) {
            val t: T? = mDatas!![position]
            if (t != null) {
                return t.itemType()
            }
        }
        return 0
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): VH {
        val layoutResId = itemTypeLayouts[viewType]
        return createBaseViewHolder(parent, layoutResId)
    }

    /**
     * 添加itemType及与它对应的布局文件到这个映射集合中；
     *
     * @param viewType
     * @param layoutResId
     */
    protected fun addItemType(viewType: Int, @LayoutRes layoutResId: Int) {
        itemTypeLayouts.put(viewType, layoutResId)
    }
}
